package com.yunxin.customui.bean.echart;


import cn.hutool.core.map.CaseInsensitiveMap;
import com.alibaba.fastjson.annotation.JSONField;
import com.yunxin.customui.myinterface.chart.IChartBuilder;
import com.yunxin.customui.util.PageUtil;
import com.yunxin.customui.bean.UIComponent;
import com.yunxin.customui.service.builder.echart.BusinessChartBuilder;
import com.yunxin.customui.service.builder.echart.SqlChartBuilder;
import com.yunxin.customui.myinterface.chart.IAxisHandler;
import com.yunxin.customui.util.ApplicationContextUtil;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.ArrayList;
import java.util.List;


/**
 * 图表的实体类
 *
 * @author LGY
 * @date 2021/10/11
 */
@Getter@Setter
@ToString
public class ChartBean extends UIComponent {


    /**
     * chart 构造器
     */
    IChartBuilder ichartBuilder;

    /**
     * 表id
     */
    Integer chartId;
    /**
     * 标题
     */
    String title;
    /**
     * 图例
     */
    Legend legend;

    /**
     * 提示
     */
    Tooltip tooltip;

    /**
     * 构造函数
     *
     * @param chartCfgBean 配置
     */
    public ChartBean(ChartCfgBean chartCfgBean) {
      super(chartCfgBean.getComponentType(),chartCfgBean.getComponentId());
      if (chartCfgBean.getChartID()%10==3){
          ichartBuilder = new SqlChartBuilder();
      }else {
          ichartBuilder = new BusinessChartBuilder();
      }

    }

    /**
     * x轴
     */
    @JSONField(name = "xAxis")
    List<Axis> xAxis;

    /**
     * y轴
     */
    @JSONField(name = "yAxis")
    List<Axis> yAxis;

    /**
     * 系列
     */
    List<Series> series = new ArrayList<>();

    /**
     * 构建
     *
     * @param config  配置
     * @param dataMap 数据
     */
    public void build(ChartSqlConfig config, List<CaseInsensitiveMap<String, Object>> dataMap) {
        IChartBuilder.ChartData chartData = ichartBuilder.buildChart(this, config, dataMap);
        for (ChartValCfgBean chartValCfgBean : config.getChartValCfgBeans()) {
            List<String> list = chartData.getDataMap().get(chartValCfgBean.getDataID());
            this.series.add(new Series<>(chartValCfgBean,list));
        }

        this.chartId=config.getChartID();
        //创建图例
        //chartBean.setLegend(new Legend(config));
        //生成x、y轴 这里暂时省缺配置，默认y轴为数据轴
        this.yAxis=Axis.getYAxis(config);
        this.xAxis = Axis.getXAxis(config,chartData.getDataMap().get(config.getXField()));
        this.tooltip = new ChartBean.Tooltip(config);
        lastHandler(config);
    }

    /**
     * 最后的处理程序
     *
     * @param config 配置
     */
    public void lastHandler(ChartSqlConfig config){
        String xHandler = config.getXHandler();
        if (xHandler != null && !xHandler.isEmpty()){
            PageUtil.MethodParam methodParam = PageUtil.analysisMethod(xHandler).get(0);
            IAxisHandler<String[]> iAxisHandler = (IAxisHandler<String[]>) ApplicationContextUtil.getObject(methodParam.getMethodName());
            iAxisHandler.axisHandle(this.xAxis.get(0),methodParam.getParams());
        }
    }

    /**
     * 获得组件id
     *
     * @return {@link Integer}
     */
    @Override
    public Integer getComponentId() {
        return this.chartId;
    }


    /**
     * 图例
     *
     * @author LGY
     * @date 2021/09/17
     */
    @Data
    public static class Legend {
        /**
         * 传说
         *
         * @param chartCfgBean 图表cfg豆
         *///创建图例
        public Legend(ChartCfgBean chartCfgBean) {
            ArrayList<String> names = new ArrayList<>();
            for (ChartValCfgBean chartValCfgBean : chartCfgBean.getChartValCfgBeans()) {
                names.add(chartValCfgBean.getChartName());
            }
            this.data = names;
        }

        /**
         * 数据
         */
        ArrayList<String> data;
    }


    /**
     * 工具提示
     *
     * @author LGY
     * @date 2021/10/11
     */
    @Data
    public static class Tooltip{
        /**
         * 构造方法
         *
         * @param chartCfgBean 图配置
         */
        public Tooltip(ChartCfgBean chartCfgBean) {
            this.trigger = chartCfgBean.tooltipTrigger;
            this.axisPointer.type = chartCfgBean.axisPointerType;
        }

        /**
         * 触发
         */
        String trigger;
        /**
         * 轴的指针
         */
        AxisPointer axisPointer = new AxisPointer();
    }

    /**
     * 轴的指针（鼠标）
     *
     * @author LGY
     * @date 2021/10/11
     */
    @Data
    public static class AxisPointer{
        /**
         * 类型
         */
        String type = "shadow";
    }
}
